package cemp.service;

import cemp.bean.OOMObject;
import cemp.common.api.reponse.RestCommonResponse;
import cemp.common.api.reponse.RestStockInfoTodayResponse;
import cemp.common.api.reponse.RestStockPlates;
import cemp.common.api.reponse.RestStockPriceResponse;
import cemp.common.dto.response.CommonOption;
import cemp.common.util.DateUtil;
import cemp.common.util.DateUtils;
import cemp.common.util.StockCopyPropertyUtil;
import cemp.entity.StockAll;
import cemp.entity.StockKpiDay;
import cemp.entity.StockPlate;
import cemp.mapper.StockAllMapper;
import cemp.mapper.StockKpiDayMapper;
import cemp.mapper.StockPlateMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Slf4j
public class StockCoreService {

    private final StockAllMapper stockAllMapper;
    private final StockKpiDayMapper stockKpiDayMapper;
    private final StockPlateMapper stockPlateMapper;
    private final StockCommonUtil stockCommonUtil;

    public void oom(){
        System.out.println("oom begin");
        int index = 0;
        List<OOMObject> list = new ArrayList<>();
        if(1==1){
            while (true){
                OOMObject oom = new OOMObject();
                oom.setInt_1m(1024 * 1024);
                list.add(oom);
                index ++;
            }
        }
    }

    public void exceptiopn(){
        stockAllMapper.exception();
    }

    public RestCommonResponse getStockDetail(String stockCode) {
        LambdaQueryWrapper<StockKpiDay> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StockKpiDay::getStockCode,stockCode);
        List<StockKpiDay> list =  stockKpiDayMapper.selectList(lqw);
        RestCommonResponse response = new RestCommonResponse();
        //
        List<List<String>> result = new ArrayList<>();
        result = list.stream().map(new Function<StockKpiDay, List<String>>() {
            @Override
            public List<String> apply(StockKpiDay stockKpiDay) {
                List<String> kpiDayList = new ArrayList<>();
                kpiDayList.add(DateUtil.date2Str(stockKpiDay.getStaDate()));
                kpiDayList.add(stockKpiDay.getPriceBegin().toPlainString());
                kpiDayList.add(stockKpiDay.getPriceEnd().toPlainString());
                kpiDayList.add(stockKpiDay.getPriceMin().toPlainString());
                kpiDayList.add(stockKpiDay.getPriceMax().toPlainString());
                return kpiDayList;
            }
        }).collect(Collectors.toList());
        response.setData(result);
        return response;
    }


    public RestCommonResponse getPlateOptions() {
        RestCommonResponse response = new RestCommonResponse();
        List<StockPlate> result =  stockPlateMapper.selectList(null);
        response.setData(result.stream().map(plate -> new CommonOption(plate.getPlatecode(),plate.getPlatecode(),plate.getName())).collect(Collectors.toList()));
        return response;
    }

    public RestCommonResponse getInfoToday(String stockCode) {
        RestCommonResponse response = new RestCommonResponse();
        List<RestStockInfoTodayResponse> list = new ArrayList<>();
        RestStockInfoTodayResponse todayResponse = new RestStockInfoTodayResponse();

        //基本信息
        LambdaQueryWrapper<StockAll> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StockAll::getStockCode,stockCode);
        List<StockAll> result = stockAllMapper.selectList(lqw);
        if(result != null && result.size() > 0){
            BeanUtils.copyProperties(result.get(0),todayResponse);
        }
        //板块信息
        todayResponse.setPlates(stockPlateMapper.getPlatesByCode(stockCode).stream()
                .map(stockPlate -> new StockCopyPropertyUtil<RestStockPlates>().rtnBean(stockPlate,new RestStockPlates())).collect(Collectors.toList()));
        //当日股价信息
        RestStockPriceResponse price = new RestStockPriceResponse();
        StockKpiDay priceLastDay =  stockKpiDayMapper.findOneLastDay(stockCode);
        if(priceLastDay != null){
            BeanUtils.copyProperties(priceLastDay,price);
            todayResponse.setPrice(price);
        }
        list.add(todayResponse);
        response.setData(list);
        return response;
    }

    /**
     * 计算均线
     */
    public void calAvg(){
        List<StockAll> list = stockCommonUtil.queryAllStocks();
        list.forEach(new Consumer<StockAll>() {
            @Override
            public void accept(StockAll stockAll) {

            }
        });
    }
}
